use "E:\研究\yuqi合作\官员更替对技术演化\data.dta"
cd "E:\研究\城市形态对技术演化"
global control l.ln年末总人口   l.ln国内生产总值  l.ln外商实际投资额 
*************************************************************************************************************************************
*********************************Summary statisticbaseline***************************************************************************
*************************************************************************************************************************************
help asdoc
asdoc sum entry1 density gci u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10 l.ln年末总人口   l.ln国内生产总值  l.ln外商实际投资额  ///
if l.rca==0&!missing(entry1, density, gci, u_area_am_10000, u_ai_100, u_shape_am_10, u_prox_am_10, l.ln年末总人口,   l.ln国内生产总值,  l.ln外商实际投资额, u_area_am_10000_density  )



*************************************************************************************************************************************
************************************************Table 1 and Table A1:Effects on technological entries********************************
*************************************************************************************************************************************
erase "result\base.xlsx"
erase "result\base.txt"
foreach var of varlist   u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10 {
reghdfe entry1 l.`var' $control if l.rca==0,noabsorb vce(r)
outreg2  using "result\base.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, NO, Year FE, NO) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' $control if l.rca==0,absorb(city ipc4 year)  vce(r)
outreg2  using "result\base.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' l.density l.gci l.gci_sq   $control if l.rca==0,absorb(city ipc4 year)  vce(r)
outreg2  using "result\base.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

}

*************************************************************************************************************************************
***************************************************Tables 2 Effects on complex technological diversification*************************
*************************************************************************************************************************************
erase "result\complexity.xlsx"
erase "result\complexity.txt"
foreach var of varlist  u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10 {
reghdfe entry1 l.`var' l.`var'_gci  l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)   vce(r)
outreg2  using "result\complexity.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}


*************************************************************************************************************************************
***************************************Tables 3 Effects on path-breaking technological diversification*******************************
*************************************************************************************************************************************
erase "result\density.xlsx"
erase "result\density.txt"
foreach var of varlist   u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10 {
reghdfe entry1 l.`var' l.`var'_density l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)  vce(r)
outreg2  using "result\density.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}



*************************************************************************************************************************************
*******************Fig. 4 Heterogeneity Analysis: internet infrastructure as a moderator.********************************************
*************************************************************************************************************************************
erase "result\net2.xlsx"
erase "result\net2.txt"

foreach var of varlist u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10{

reghdfe entry1 l.`var' l.`var'_lnnet l.lnnet l.density l.gci l.gci_sq   $control if l.rca==0,absorb(city ipc4 year)   vce(r)
outreg2  using "result\net2.xlsx",excel append   drop(i.year i.区_id)  adec(8) bdec(8) sdec(8) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' l.`var'_gci l.`var'_gci_lnnet l.lnnet l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)    vce(r)
outreg2  using "result\net2.xlsx",excel append   drop(i.year i.区_id)  adec(8) bdec(8) sdec(8) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' l.`var'_density l.`var'_density_lnnet l.lnnet l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)    vce(r)
outreg2  using "result\net2.xlsx",excel append   drop(i.year i.区_id) adec(8) bdec(8) sdec(8) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}
*************************************************************************************************************************************
*************************************Robustness checks*******************************************************************************
*************************************************************************************************************************************

*******************************************Table A4a–b substituting the original urban form indices***********************************
erase "result\robust1.xlsx"
erase "result\robust1.txt"

foreach var of varlist  u_np2 u_connect_1000  u_frac_am u_pafrac u_contig_am {

reghdfe entry1 l.`var' l.`var'_gci  l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)    vce(r)
outreg2  using "result\robust1.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' l.`var'_density l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)    vce(r)
outreg2  using "result\robust1.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}


*******************************************Table A5. Alternative Entry specifications************************************
erase "result\entry2l2.xlsx"
erase "result\entry2l2.txt"

foreach var of varlist   u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10{

reghdfe entry2 l2.`var' l2.density l2.gci l2.gci_sq   $control if l.rca==0&l2.rca==0,absorb(city ipc4 year)   vce(r)
outreg2  using "result\entry2l2.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry2 l2.`var' l2.`var'_gci  l2.density l2.gci l2.gci_sq $control if l.rca==0&l2.rca==0,absorb(city ipc4 year)    vce(r)
outreg2  using "result\entry2l2.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry2 l2.`var' l2.`var'_density l2.density l2.gci l2.gci_sq $control if l.rca==0&l2.rca==0,absorb(city ipc4 year)    vce(r)
outreg2  using "result\entry2l2.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}
****************************************Table A6a-b: Alternative model types************************************
set matsize 10000

erase "result\probit_base.xlsx"
erase "result\probit_base.txt"

foreach var of varlist   u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10  u_contig_am u_prox_am_1000{
probit entry1 l.`var' l.density l.gci l.gci_sq   $control i.citynumber  i.patentid  i.year  if l.rca==0
outreg2  using "result\probit_base.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}

erase "result\probit_gci.xlsx"
erase "result\probit_gci.txt"


foreach var of varlist   u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10  u_contig_am u_prox_am_1000{
probit entry1 l.`var' l.`var'_gci  l.density l.gci l.gci_sq $control  i.citynumber  i.patentid  i.year if l.rca==0
outreg2  using "result\probit_gci.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}

erase "result\probit_density.xlsx"
erase "result\probit_density.txt"

foreach var of varlist   u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10{
probit entry1 l.`var' l.`var'_density l.density l.gci l.gci_sq $control  i.citynumber  i.patentid  i.year  if l.rca==0
outreg2  using "result\probit_density.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}


*************************************A7: restrict the sample to the period from 2001 to 2016************************************

erase "result\robust2016.xlsx"
erase "result\robust2016.txt"
foreach var of varlist  u_area_am_10000 u_ai_100 u_shape_am_10 u_prox_am_10{

reghdfe entry1 l.`var' l.density l.gci l.gci_sq   $control if l.rca==0,absorb(city ipc4 year) cluster(uid)
outreg2  using "result\robust2016.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' l.`var'_gci  l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)   cluster(uid)
outreg2  using "result\robust2016.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')

reghdfe entry1 l.`var' l.`var'_density l.density l.gci l.gci_sq $control if l.rca==0,absorb(city ipc4 year)   cluster(uid)
outreg2  using "result\robust2016.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var')
}



**************************************************Table A8a–b:IV Regression Model***********************************************************************


global control2 ln年末总人口   ln国内生产总值  ln外商实际投资额 

erase "result\iv_1st.xlsx"
erase "result\iv_1st.txt"

foreach var of varlist   u_area  u_ai u_shape u_prox_am{
reghdfe `var' `var'_nbmean_kernelk12 density gci gci_sq $control2
outreg2  using "result\iv_1st.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var'_1st)

ivreghdfe entry1 l.density l.gci l.gci_sq $control (l.`var'=l.`var'_nbmean_kernelk12 ) ///
if l.rca==0,absorb(city ipc4 year) vce(robust)
outreg2  using "result\iv_1st.xlsx",excel append   drop(i.year i.区_id)  adec(4) bdec(4) sdec(4) ///
addtext(IPC FE, YES, City FE, YES, Year FE, YES) e( r2_a ) ctitle(`var'_2nd)
}


foreach var of varlist  u_ai {
ivreghdfe entry1 l.density l.gci l.gci_sq $control (l.`var'=l.`var'_nbmean_kernelk12  ) ///
if l.rca==0,absorb(city ipc4 year)  vce(robust)
}

